package controller

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo/v4"
	"law_case_api/dto"
	"law_case_api/models"
	"law_case_api/services"
	"law_case_api/utils"
	"net/http"
	"strconv"
)

// RouteList
// @Summary 获取所有的路由信息，并查询当前登录人的路由信息
// @Tags 系统管理-路由管理
// @Accept plain
// @Produce plain
// @Success 200 {object} models.BaseResult
// @Failure 400 {object} models.BaseResult
// @Router /wzj/system/route/list [GET]
func RouteList(c echo.Context) error {
	resp := models.BaseResult{
		Code: 200,
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*models.JwtCustomClaims)
	userid, _ := strconv.Atoi(claims.UserId)
	if userid == 0 {
		resp.Code = 400
		resp.Message = "用户信息不存在，请重新登录"
		return c.JSON(http.StatusOK, resp)
	}
	isLaw := false
	LoginType := c.Request().Header["Logintype"]
	if len(LoginType) > 0 {
		if LoginType[0] == "1" {
			isLaw = true
		}
	}
	caseListReq := dto.CaseListReq{}
	if err := c.Bind(&caseListReq); err != nil {
		resp.Code = 400
		resp.Message = err.Error()
		return c.JSON(http.StatusBadRequest, resp)
	}
	if caseListReq.PageIndex == 0 {
		caseListReq.PageIndex = 1
	}
	if caseListReq.PageSize == 0 {
		caseListReq.PageSize = 10
	}
	_case := services.CaseService{}
	respData, respPageCount, err := _case.GetCaseListByCondition(userid, isLaw, caseListReq)
	if err != nil {
		resp.Code = 400
		return c.JSON(http.StatusOK, resp)
	}
	resp.PageCount = respPageCount
	var data []dto.CaseInfoReq
	utils.MapTo(respData, &data)
	resp.Data = data

	return c.JSON(http.StatusOK, resp)
}
